#include "mplayer.h"

MPlayer::MPlayer()
{
    /*init config file*/
    QString iniFilePath = QCoreApplication::applicationDirPath() + "/config.ini";
    if(!QFile(iniFilePath).exists())
    {
        QMessageBox::critical(nullptr, "ERROR",
                              iniFilePath + " is not exist.");
        qCritical() << iniFilePath + " is not exist.";
    }
    settings = new QSettings(iniFilePath, QSettings::IniFormat);
    settings->setIniCodec("UTF-8");
    /*init class number*/
    fifo_file_path = settings->value("MPLAYER/fifo_file_path").toString();
    mplayerPath = settings->value("MPLAYER/mplayer_path").toString();
    playUrl = settings->value("MPLAYER/play_url").toString();
    qDebug() << QString(fifo_file_path);
    qDebug() << QString(mplayerPath);
    qDebug() << QString(playUrl);

    /*init player width and height of param*/
    width = 800;
    height = 480;

    /*init player process*/
    process = new QProcess;
    connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
            this, &MPlayer::finished);

    /*init fifo file*/
    mplayer_fifo = init_mplayer_fifo();
    if (mplayer_fifo == -1)
    {
        qCritical() << "init_mplayer_fifo open mplayer_fifo fail: " << QString(strerror(errno));
        QMessageBox::critical(nullptr, "ERROR", "init_mplayer_fifo open mplayer_fifo fail");
        close(mplayer_fifo);
    }
    else
    {
        qDebug() << "init mplayer fifo file succeed.";
    }
}

MPlayer::~MPlayer()
{
    delete process;
    delete settings;
}

int MPlayer::cmdWrite(QString cmdString)
{
    qDebug() << "write to fifo file to string of " << cmdString;
    return write(mplayer_fifo, cmdString.toStdString().c_str(),
                 strlen(cmdString.toStdString().c_str()));
}

void MPlayer::pause()
{
    if(!playFlag)
        return;
    playFlag = false;
    int ret;
    ret = write(mplayer_fifo, "pause\n", strlen("pause\n"));
    if(ret == -1)
    {
        qCritical() << "mplayer write error: " << QString(strerror(errno));
        return;
    }
    qDebug() << "Mplayer pause";
}

void MPlayer::go_on()
{
    if(playFlag)
        return;
    playFlag = true;
    int ret;
    ret = write(mplayer_fifo, "pause\n", strlen("pause\n"));
    if(ret == -1)
    {
        qCritical() << "mplayer write error: " << QString(strerror(errno));
        return;
    }
    qDebug() << "Mplayer go on";
}

bool MPlayer::isPlay()
{
    return playFlag;
}

void MPlayer::start()
{
    QString cmdString = QString("%1 -slave -quiet -input file=%2"
                                " %3 -zoom -x %4 -y %5 -loop 0 ")
            .arg(mplayerPath)
            .arg(fifo_file_path)
            .arg(playUrl)
            .arg(width)
            .arg(height);
    process->start(cmdString);
    playFlag = true;
    qDebug() << "start for command line to " << cmdString;
}

void MPlayer::finished()
{

}

int MPlayer::init_mplayer_fifo(void)
{
    qDebug() << "fifo_file_path: " << QString(fifo_file_path.toStdString().c_str());
    int err_number;
    err_number = unlink(fifo_file_path.toStdString().c_str());//如果文件存在，则先删除
    if(err_number != 0)
        qCritical() << "unlink fifofile error: " << QString(strerror(errno));
    err_number = mkfifo(fifo_file_path.toStdString().c_str(), O_CREAT | 0666); //创建命名管道
    if(err_number != 0)
        qCritical() << "mkfifo fifofile error: " << QString(strerror(errno));
    int mplayer_fifo = open("/tmp/mplayer_fifo", O_RDWR);
    return mplayer_fifo;
}

void MPlayer::setWidthHeight(int width, int height)
{
    this->width = width;
    this->height = height;
}
